package com.wondersgroup.framework.sample.component.tree.action;

import com.wondersgroup.framework.core.web.struts2.action.BaseAjaxAction;
import com.wondersgroup.framework.core.web.vo.VOUtils;
import com.wondersgroup.framework.core5.model.vo.ValueObject;
import com.wondersgroup.framework.sample.component.data.TreeData;
import com.wondersgroup.framework.sample.component.tree.vo.CheckTreeVo;
import com.wondersgroup.framework.sample.component.tree.vo.TreeFormVo;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class TreeUpdateAction extends BaseAjaxAction {
	private HashMap<CheckTreeVo, String> treeData;
	private TreeFormVo treeFormVo;
	private static final String prefix = "{\"success\":true,result:[";
	private static final String suffex = "]}";

	public TreeUpdateAction() {
		this.treeData = new HashMap();

		this.treeFormVo = new TreeFormVo();
	}

	public ValueObject getValueObject() {
		return this.treeFormVo;
	}

	public TreeFormVo getTreeFormVo() {
		return this.treeFormVo;
	}

	public void setTreeFormVo(TreeFormVo treeFormVo) {
		this.treeFormVo = treeFormVo;
	}

	public String loadTreeForm() {
		CheckTreeVo CheckTreeVo = new CheckTreeVo();
		String nodeid = super.getServletRequest().getParameter("id");
		String nodetext = super.getServletRequest().getParameter("text");
		CheckTreeVo.setId(nodeid);
		CheckTreeVo.setText(nodetext);
		String json = "{\"success\":true,result:["
				+ VOUtils.getJsonData(CheckTreeVo) + "]}";
		createJSonData(json);
		return "ajax";
	}

	public String addTreeForm() {
		return "ajax";
	}

	public String delTreeFrom() {
		this.treeData = getTreeData();
		boolean isleaf = true;
		String nodeid = super.getServletRequest().getParameter("id");
		String parentId = getServletRequest().getParameter("parentId");

		Iterator it = this.treeData.keySet().iterator();
		label243: while (it.hasNext()) {
			CheckTreeVo key = (CheckTreeVo) it.next();
			if (nodeid.equals(key.getId())) {
				if (key.isLeaf()) {
					this.treeData.remove(key);

					Iterator iter = this.treeData.values().iterator();

					while (iter.hasNext()) {
						String value = (String) iter.next();
						if (parentId.equals(value)) {
							isleaf = false;
							break;
						}
					}

					if (!(isleaf))
						break;
					it = this.treeData.keySet().iterator();
					while (true) {
						if (!(it.hasNext()))
							break label243;
						CheckTreeVo pkey = (CheckTreeVo) it.next();
						if (parentId.equals(pkey.getId()))
							pkey.setLeaf(true);

					}

				}

				try {
					this.servletResponse.getWriter().write("{success:false}");
				} catch (IOException e) {
					e.printStackTrace();
				}
				return null;
			}

		}

		label243: setTreeData(this.treeData);
		return "ajax";
	}

	public String saveTreeForm() {
		Iterator it;
		CheckTreeVo key;
		this.treeData = getTreeData();
		String id = getServletRequest().getParameter("id");
		String text = getServletRequest().getParameter("text");
		String parentId = getServletRequest().getParameter("parentId");

		if (parentId.equals("")) {
			it = this.treeData.keySet().iterator();
			while (it.hasNext()) {
				key = (CheckTreeVo) it.next();
				if (id.equals(key.getId()))
					key.setText(text);

			}

		} else {
			if (id.equals("")) {
				try {
					this.servletResponse.getWriter().write("{success:false}");
				} catch (IOException e) {
					e.printStackTrace();
				}
				return null;
			}

			it = this.treeData.keySet().iterator();
			while (it.hasNext()) {
				key = (CheckTreeVo) it.next();
				if (id.equals(key.getId())) {
					try {
						this.servletResponse.getWriter().write(
								"{success:false}");
					} catch (IOException e) {
						e.printStackTrace();
					}
					return null;
				}

			}

			it = this.treeData.keySet().iterator();
			while (it.hasNext()) {
				key = (CheckTreeVo) it.next();
				if ((parentId.equals(key.getId())) && (key.isLeaf())) {
					key.setLeaf(false);
				}

			}

			this.treeData
					.put(new CheckTreeVo(id, text, false, true, false, ""),
							parentId);
		}
		setTreeData(this.treeData);
		return "ajax";
	}

	public HashMap<CheckTreeVo, String> getTreeData() {
		return new TreeData(this.servletRequest).getTreeData();
	}

	public void setTreeData(HashMap<CheckTreeVo, String> treeData) {
		getServletRequest().getSession().getServletContext()
				.setAttribute("treeList", treeData);
	}
}